package android.bkav.testing.activity;

import android.app.ListActivity;
import android.bkav.testing.R;
import android.bkav.testing.utils.DatabaseNoteAdapter;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;

public class ListNotesActivity extends ListActivity {
	
	DatabaseNoteAdapter mDatabaseNoteAdapter;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_notes_screen);
		
		mDatabaseNoteAdapter = new DatabaseNoteAdapter(this);
		mDatabaseNoteAdapter.open();
		
		fillData();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case R.id.new_note:
			Intent i = new Intent(ListNotesActivity.this, CreateNewNoteActivity.class);
			startActivity(i);
		}
		return true;
	}
	
	//Get all of the notes form database and create the item list
	public void fillData(){
		Cursor cur = mDatabaseNoteAdapter.fetchAllDbNotes();
		
		startManagingCursor(cur);
		
		String[] from = new String[]{DatabaseNoteAdapter.KEY_TITLE, DatabaseNoteAdapter.KEY_CONTENT, DatabaseNoteAdapter.KEY_TIME_DATE};
		int[] to = new int[]{R.id.txtNote_Title, R.id.txtNote_Content, R.id.txtDate_Time_Modified};
		Log.i("den day chua?", "roi");
		Log.i("from", String.valueOf(from.length));
		
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.a_note, cur, from, to);
		
		setListAdapter(notes);
	}
	
	public void onClickItemsListView(){
		
	}
}
